import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:card_swiper/card_swiper.dart';
import 'package:self32/page/borrow_page.dart';
import 'package:self32/page/mine_page.dart';
import 'package:self32/page/return_page.dart';

import '../main.dart';
import '../widget/focus_killer.dart';
import 'borrow_again_page.dart';
import 'setting_page.dart';

class HfhIndex extends StatefulWidget {
  const HfhIndex({Key? key}) : super(key: key);

  @override
  State<HfhIndex> createState() => _HfhIndexState();
}

class _HfhIndexState extends State<HfhIndex> {

  final RxList<String> bannerList = RxList();

  @override
  initState() {
    super.initState();

    _loadBanner();
  }

  _loadBanner() {

    bannerList.add("assets/images/hfh_banner.png");
  }

  @override
  void dispose() {
    clock.cancel();
    super.dispose();
  }

  _swiper(){
    return Container(
      height: 160.h,
      margin: EdgeInsets.only(left: 8.r,right: 8.r),
      child: Obx(() => Swiper(
        itemBuilder: (BuildContext context,int index){
          return Container(
            clipBehavior: Clip.hardEdge,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
            ),
            // child: Image.network("$apiUrl/${bannerList[index]}", fit: BoxFit.cover,),
            child: Image.asset(bannerList[index])
          );
        },
        itemCount: bannerList.length,
        itemWidth: 380.w,
        itemHeight: 160.h,
        autoplay: true,
        autoplayDelay: 5000,
        // layout: SwiperLayout.STACK,
        pagination: const SwiperPagination(),
        // control: SwiperControl(),
      )),
    );
  }

  _action(){
    return Container(
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/hfh_bg.png",
            ),
          ),
        ),
      padding: EdgeInsets.only(left: 6.r,right: 6.r,top: 8.r,bottom: 8.r),
      margin: EdgeInsets.only(left: 8.r,right: 8.r,top: 20.r),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              InkWell(
                onTap: (){
                  Get.to(()=> const Borrow());
                },
                child: Image.asset("assets/images/hfh_borrow.png",width:188.w,fit: BoxFit.fitWidth,),
              ),
              InkWell(
                onTap: (){
                  Get.to(()=> const Return());
                },
                child: Image.asset("assets/images/hfh_return.png",width:188.w,fit: BoxFit.fitWidth,),
              ),
            ],
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              InkWell(
                onTap: (){
                  Get.to(()=> const BorrowAgain());
                },
                child: Image.asset("assets/images/hfh_borrow_again.png",width:188.w,fit: BoxFit.fitWidth,),
              ),

              InkWell(
                onTap: (){
                  Get.to(()=> const Mine());
                },
                child: Image.asset("assets/images/hfh_mine.png",width:188.w,fit: BoxFit.fitWidth,),
              ),
            ],
          )
        ],
      ));
  }


  _version(){
    return Container(
        margin: EdgeInsets.only(top: 6.r,left: 8.r,right: 8.r),
        child: Align(child: Text('版本号$version',style: TextStyle(color: Colors.white,fontSize: 8.sp)),)
    );
  }

  _appBar(){
    return Container(
        width: double.infinity,
        height: 48.h,
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/hfh_top_bg.png",
            ),
          ),
        ),
        padding: EdgeInsets.only(left: 8.r,right: 8.r),
        alignment: Alignment.center,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset("assets/images/hfh_logo.png",width: 50.w,fit: BoxFit.fitWidth,),
            Row(
              children: [
                InkWell(
                  onTap:(){
                    Get.to(()=>const Setting());
                  },
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset("assets/images/hfh_setting.png",height: 16.h,fit: BoxFit.fitHeight,),
                      Text('系统设置',style: TextStyle(color: Colors.white,fontSize: 6.sp),)
                    ],
                  )
                ),
                SizedBox(width: 10.w,),
                InkWell(
                  onTap:(){
                    Get.defaultDialog(
                      title: '退出',
                      titleStyle: TextStyle(
                        color: const Color(0xFF000000),
                        fontSize: 10.sp,
                      ),
                      middleText: '确定退出应用？',
                      middleTextStyle: TextStyle(
                        color: const Color(0xFF000000),
                        fontSize: 8.sp,
                      ),
                      radius: 4.sp,
                      textConfirm: '确定',
                      // textCancel: '取消',
                      confirmTextColor: const Color(0xFF007AFF),
                      backgroundColor: const Color(0xD1F8F8F8),
                      buttonColor: Colors.transparent,
                      onConfirm: () {
                        SystemNavigator.pop();
                        exit(0);
                      },
                    );
                  },
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset("assets/images/hfh_quit.png",height: 16.h,fit: BoxFit.fitHeight,),
                      Text('退出',style: TextStyle(color: Colors.white,fontSize: 6.sp),)
                    ],
                  )
                ),
              ],
            ),
          ],
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
              width: double.infinity,
              height: double.infinity,
              decoration: const BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: AssetImage(
                    "assets/images/hfhbg.png",
                  ),
                ),
              ),
              child: Column(
                children: [
                  _appBar(),
                  Align(child: focusKiller(),),
                  Padding(
                    padding: EdgeInsets.only(top: 16.r,bottom: 18.r),
                    child: Text('欢迎使用图书馆自助借还系统',style: TextStyle(fontSize: 28.sp,fontWeight: FontWeight.bold,color: Colors.white),),
                  ),
                  _swiper(),
                  _action(),
                  _version()
                ],
              )
          ),

        ]
      )
    );
  }
}